﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
using System.Runtime.InteropServices;
using Emgu.CV.CvEnum;
namespace Test
{
    public partial class Form2 : Form
    {
        static string filename;
        public Form2()
        {

            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                IntPtr ptrImage = CvInvoke.cvLoadImage(open.FileName, LOAD_IMAGE_TYPE.CV_LOAD_IMAGE_UNCHANGED);
                /* IplImage* 转换为Bitmap */
                pictureBox1.Image = ConvertIntPrToBitmap(ptrImage);

                //显示灰度图像
                IntPtr grayImage = CvInvoke.cvCreateImage(CvInvoke.cvGetSize(ptrImage), IPL_DEPTH.IPL_DEPTH_8U, 1);
                CvInvoke.cvCvtColor(ptrImage, grayImage, COLOR_CONVERSION.CV_BGR2GRAY);

                //pictureBox2.Image = ConvertIntPrToBitmap(grayImage); //用该方法灰度图像会有三个图片
                MIplImage mi = (MIplImage)Marshal.PtrToStructure(grayImage, typeof(MIplImage));
                Image<Gray, Byte> image = new Image<Gray, Byte>(mi.width, mi.height, mi.widthStep, mi.imageData);


                Image<Gray, Byte> imageThreshold = image.ThresholdBinary(new Gray(150), new Gray(255)); //对灰度图像二值化
                pictureBox2.Image = imageThreshold.ToBitmap();

                CvInvoke.cvReleaseImage(ref ptrImage);
                CvInvoke.cvReleaseImage(ref grayImage);



            }

            /*
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                filename = open.FileName;
                pictureBox1.Load(filename);
                test();
            }
            */
        }


        public static Bitmap ConvertIntPrToBitmap(IntPtr ptrImage)
        {
            //将IplImage指针转换成MIplImage结构
            MIplImage mi = (MIplImage)Marshal.PtrToStructure(ptrImage, typeof(MIplImage));
            Image<Bgr, Byte> image = new Image<Bgr, Byte>(mi.width, mi.height, mi.widthStep, mi.imageData);
            return image.ToBitmap();
        }

        private void test()
        {
            Image<Bgr, byte> src = new Image<Bgr, byte>(filename);
            Image<Gray, byte> img_1 = new Image<Gray, byte>(src.Size);
            CvInvoke.cvCvtColor(src, img_1, Emgu.CV.CvEnum.COLOR_CONVERSION.CV_BGR2GRAY);
            CvInvoke.cvThreshold(img_1, img_1, 100, 255, Emgu.CV.CvEnum.THRESH.CV_THRESH_BINARY);
            IntPtr Dyncontour = new IntPtr();//存放检测到的图像块的首地址

            IntPtr Dynstorage = CvInvoke.cvCreateMemStorage(0);//开辟内存区域
            Seq<Point> DyncontourTemp1 = new Seq<Point>(Dyncontour, null);//方便对IntPtr类型进行操作

            Seq<Point> DyncontourTemp = DyncontourTemp1;

            int m;
            m = 1000;
            int n = CvInvoke.cvFindContours(img_1, Dynstorage, ref Dyncontour, m, Emgu.CV.CvEnum.RETR_TYPE.CV_RETR_TREE, Emgu.CV.CvEnum.CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_SIMPLE, new Point(0, 0));
            for (; DyncontourTemp != null && DyncontourTemp.Ptr.ToInt32() != 0; DyncontourTemp = DyncontourTemp.HNext)
            {

                CvInvoke.cvDrawContours(img_1, DyncontourTemp, new MCvScalar(100, 100, 100), new MCvScalar(200, 200, 200), 3, 3, Emgu.CV.CvEnum.LINE_TYPE.FOUR_CONNECTED, new Point(0, 0));

            }
            pictureBox2.Image = img_1.ToBitmap();
        }
    }
}
